package com.wonders.common.ws.util;

import java.text.DecimalFormat;

import org.apache.commons.lang3.StringUtils;

import com.wonders.common.config.manager.instance.PropertiesManager;
import com.wonders.common.ws.constant.BusiCode;
import com.wonders.common.ws.constant.TradeCode;
import com.wonders.common.ws.entity.vo.XtptMsg;
import com.wonders.common.ws.mapping.bean.global.Head;
import com.wonders.common.ws.mapping.bean.global.Rst;
import com.wonders.framework.service.BaseService;
import com.wonders.framework.util.DateUtil;
import com.wonders.framework.util.web.ApplicationUtil;

/**
 * 协同平台工具
 * 
 * @author WuBin
 * 
 */
public class XtptUtil {

	private final static DecimalFormat decimalFormatRef = new DecimalFormat(
			"00000000");

	/**
	 * 生成请求报文头
	 * 
	 * @param key
	 *            业务主键
	 * @return 报文头
	 */
	public static Head generateRequestHead(String key) {
		String dateString = DateUtil.toChar();
		String[] code = key.split("_");
		Head globalHead = new Head();
		globalHead.setVersion("1.0.0");
		globalHead.setRef(StringUtils.join(
				ApplicationUtil.getBean(PropertiesManager.class)
						.getXtptSysCode(), dateString.substring(0, 8),
				decimalFormatRef.format(ApplicationUtil.getBean(
						BaseService.class).getSequence("seq_ref"))));
		globalHead.setSysCode(code[0]);
		globalHead.setBusCode(code[1]);
		globalHead.setTradeSrc(ApplicationUtil.getBean(PropertiesManager.class)
				.getXtptSysCode().substring(0, 1));
		globalHead.setSender(ApplicationUtil.getBean(PropertiesManager.class)
				.getXtptSysCode());
		globalHead.setReciver(code[0]);
		globalHead.setReSnd("N");
		globalHead.setDate(dateString.substring(0, 8));
		globalHead.setTime(dateString.substring(8, 14));
		Rst rst = new Rst();
		rst.setTradeCode(TradeCode.DEFAULT);
		rst.setBusiCode(BusiCode.DEFAULT);
		globalHead.setRst(rst);
		return globalHead;
	}

	/**
	 * 生成响应报文头
	 * 
	 * @param reqHead
	 *            请求报文头
	 * @param response
	 *            响应数据
	 * @return 报文头
	 */
	public static Head generateResponseHead(Head reqHead, XtptMsg response) {
		if (reqHead == null) {
			String dateString = DateUtil.toChar();
			reqHead = new Head();
			reqHead.setVersion("1.0.0");
			reqHead.setRef(StringUtils.join("O00000",
					dateString.substring(0, 8), "00000000"));
			reqHead.setSysCode(ApplicationUtil.getBean(PropertiesManager.class)
					.getXtptSysCode());
			reqHead.setBusCode("XXXXXX");
			reqHead.setTradeSrc("O");
			reqHead.setSender(ApplicationUtil.getBean(PropertiesManager.class)
					.getXtptSysCode());
			reqHead.setReciver("O00000");
			reqHead.setReSnd("N");
			reqHead.setDate(dateString.substring(0, 8));
			reqHead.setTime(dateString.substring(8, 14));
			Rst rst = new Rst();
			reqHead.setRst(rst);
		} else {
			String sender = reqHead.getSender();
			String reciver = reqHead.getReciver();
			reqHead.setSender(reciver);
			reqHead.setReciver(sender);
		}
		reqHead.getRst().setTradeCode(response.getTradeCode());
		reqHead.getRst().setBusiCode(response.getBusiCode());
		reqHead.getRst().setInfo(response.getInfo());
		return reqHead;
	}

	/**
	 * 业务处理成功
	 * 
	 * @param data
	 *            数据
	 * @return 返回信息
	 */
	public static XtptMsg success(Object data) {
		XtptMsg xtptMsg = new XtptMsg();
		xtptMsg.setTradeCode(TradeCode.SUCCESS);
		xtptMsg.setBusiCode(BusiCode.SUCCESS);
		xtptMsg.setInfo("处理成功");
		xtptMsg.setBody(data);
		return xtptMsg;
	}

	/**
	 * 业务处理成功
	 * 
	 * @param data
	 *            数据
	 * @param info
	 *            提示信息
	 * @return 返回信息
	 */
	public static XtptMsg success(Object data, String info) {
		XtptMsg xtptMsg = new XtptMsg();
		xtptMsg.setTradeCode(TradeCode.SUCCESS);
		xtptMsg.setBusiCode(BusiCode.SUCCESS);
		xtptMsg.setInfo(info);
		xtptMsg.setBody(data);
		return xtptMsg;
	}

	/**
	 * 分析响应结果
	 * 
	 * @param xtptResponse
	 *            响应数据
	 * @param resHead
	 *            响应结果
	 */
	public static void parseResponseHead(XtptMsg xtptResponse, Head resHead) {
		xtptResponse.setTradeCode(resHead.getRst().getTradeCode());
		xtptResponse.setBusiCode(resHead.getRst().getBusiCode());
		xtptResponse.setInfo(resHead.getRst().getInfo());
		if (TradeCode.SUCCESS.equals(resHead.getRst().getTradeCode())
				&& BusiCode.SUCCESS.equals(resHead.getRst().getBusiCode())) {
			xtptResponse.setSuccess(true);
		}
	}

}
